package com.smartboard.network.ui;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.smartboard.network.R;
import com.smartboard.network.client.GameClient;
import com.smartboard.network.client.GameEventListenerBase;
import com.smartboard.network.game.ChessType;
import com.smartboard.network.game.IPlayGame;
import com.smartboard.network.game.IRoom;
import com.smartboard.network.game.IUser;

import java.util.List;

public class GameActivity extends AppCompatActivity {
    private MyGameEventListener mGameEventListener;
    private ListView mRoomListView;
    private AppCompatDialog mCreateGameDialog;

    private MyAdapter mAdapter;
    private static IRoom mRoom;
    private List<IPlayGame> mGames;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.network_activity_game);
        setTitle(mRoom.getName());

        mGameEventListener = new MyGameEventListener();

        mRoomListView = (ListView)findViewById(R.id.list);
        mAdapter = new MyAdapter();

        FloatingActionButton addButton = (FloatingActionButton) findViewById(R.id.add);
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showCreateGameDialog();
            }
        });

        mRoomListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                IPlayGame game = (IPlayGame)mAdapter.getItem(position);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if(mRoom != null) {
            mGames = mRoom.getGames();
            GameClient.inst().registerEventListener(mGameEventListener);
            mRoomListView.setAdapter(mAdapter);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        GameClient.inst().unregisterEventListener(mGameEventListener);
    }

    private void showCreateGameDialog() {
        if(mCreateGameDialog != null && mCreateGameDialog.isShowing())
            return;
        if(mCreateGameDialog == null) {
            switch(GameClient.inst().getChessType()) {
                case ChessType.GO:
                    mCreateGameDialog = new CreateGameDialogGo(this, mRoom);
                    break;
                case ChessType.CHINESE_CHESS:
                    mCreateGameDialog = new CreateGameDialogChineseChess(this, mRoom);
                    break;
                case ChessType.INTER_CHESS:
                    mCreateGameDialog = new CreateGameDialogInterChess(this, mRoom);
                    break;
                case ChessType.GOBANG:
                    mCreateGameDialog = new CreateGameDialogGobang(this, mRoom);
                    break;
                default:
                    return;
            }
        }
        mCreateGameDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mCreateGameDialog = null;
            }
        });
        mCreateGameDialog.show();
    }

    private class MyGameEventListener extends GameEventListenerBase {

        @Override
        public void onUserInfoChange(IUser user) {
            if(user.getRoomId() == mRoom.getId()) {
                mRoom = GameClient.inst().getRoom(user.getRoomId());
            }
        }

        @Override
        public void onGameNumChanged(IRoom room) {
            if(room.equals(mRoom)) {
                mRoom = room;
                mGames = room.getGames();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.notifyDataSetChanged();
                    }
                });
            }
        }

        @Override
        public void onRoomUserChanged(IRoom room, IUser user, boolean leave) {
            if(room.equals(mRoom)) {
                mRoom = room;
            }
        }
    }

    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mGames.size();
        }

        @Override
        public Object getItem(int i) {
            return mGames.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(GameActivity.this).inflate(
                        R.layout.network_room_list_item, null, false);
            }

            IPlayGame game = (IPlayGame)getItem(position);
            TextView nameTextView = (TextView)convertView.findViewById(R.id.name);
            nameTextView.setText(game.getName());
            return convertView;
        }
    }

    public static void enter(Activity activity, IRoom room) {
        mRoom = room;
        Intent intent = new Intent();
        intent.setClass(activity, GameActivity.class);
        activity.startActivity(intent);
    }
}
